#include "mysql_util.h"
#include <iostream>
#include <string>
#include <math.h> // wz add
#include <cstdio>
using namespace std;

//立项及分工
void input_project_message(struct Project_message *prom)
{
    cout<<"请输入项目名称："<<endl;
    cin>>prom->project_name;
    cout<<"请输入项目资金："<<endl;
    cin>>prom->project_fund;
}

//周期制定
void project_period_make(struct Project_message *prom)
{
    prom->node_num=0;
    cout<<"请输入项目开始时间(xxxx-xx-xx)："<<endl;
    cin>>prom->project_start_t;
    cout<<"请输入项目结束时间(xxxx-xx-xx)："<<endl;
    cin>>prom->project_end_t;
    cout<<"请输入项目周期(天)："<<endl;
    cin>>prom->project_period;

    string flag_input="y";
    do{
        cout<<"请输入项目测试/验收节点时间(xxxx-xx-xx)："<<endl;
        string time;
        cin>>time;
        prom->node_num++;
        //prom->test_note_time.push_back(time);//project_team_id是个vector
        
        // 将 test_note_time 入库 check_info
        
        insert_project_test(prom->project_name,time);
        cout<<"继续输入节点（y）/退出请输入其它任意键):"<<endl;
        cin>>flag_input;
    }while(flag_input=="y");
}
//过程监控
void project_process_monitor(const string& node_time_now,const string& project_name)
{
        cout << "请输入项目节点的监控结果(1表示正常/0表示异常):";
        int temp;
        cin >> temp;
        //prom->monitor_result.push_back(temp);//将监控结果存入到名为monitor_result的vector中
        int monitor_result=temp;
        cout<<"项目"<<project_name<<node_time_now<<"过程监控结果为："<<monitor_result<<endl;
        //根据node_time_now和project_name将monitor_result入库check_info1
        update_monitor_result( project_name, node_time_now,  monitor_result);
}
//项目测试
void ProjectTest(const string& node_time_now,const string& project_name)
{
    cout << "请输入本次测试时间(xxxx-xx-xx):" << endl;
    string now_time;
    cin>>now_time;
    int temp;
    if(now_time<=node_time_now)
       {
            cout<<"请输入本次测试结果（1-正常；0-异常）："<<endl;
            cin>>temp;
       }
    else
    {
        temp=-1;
    }
    int test_result=temp;
    //prom->test_result.push_back(temp);//将监控结果存入到名为test_result的vector中
    cout<<"项目"<<project_name<<node_time_now<<"测试结果为："<<test_result<<endl;
    //根据 node_time_now 和project_name将test_result入库check_info1
    update_test_result( project_name,  node_time_now, test_result);
}

//项目验收
int ProjectCheck_Accept(const int test_result,int rejectionTimes,const string& node_time_now,const string& project_name)
{
    cout << "请输入本次验收时间(xxxx-xx-xx):" << endl;
    string now_time ;
    cin>>now_time;
    int temp;
    int temptimes=rejectionTimes;
    if(now_time<=node_time_now)
    {
        if(test_result==1)
            temp=1;//测试正常，按期验收通过，验收结果为1
        else
        {
            temptimes++;
            temp=0;//测试异常，验收不通过，验收结果为0
        }
    }
    else
    {
        temptimes++;
        temp=-1;//超期验收，验收结果为-1
    }
    rejectionTimes=temptimes;
    int check_accep_result=temp;
    //prom->check_accep_result.push_back(temp);//将监控结果存入到名为check_accep_result的vector中
    cout<<"项目"<<project_name<<node_time_now<<"验收结果为："<<check_accep_result<<endl;
    cout<<"该项目到目前为止验收不合格次数："<<rejectionTimes<<endl;
    //根据node_time_now和project_name将check_accep_result入库check_info1
     update_check_accep_result( project_name, node_time_now, check_accep_result); 
    //根据project_name将rejectionTimes入库project_message
     update_rejectionTimest(project_name, rejectionTimes );

}

//结项
void ProjectPost(struct Project_message *prom)
{
    // int rejectionTimes;
    // string projectEndTime;
    
    //struct Project_message prom ;
    //根据project_name从数据库获得该项目的project_end_t、rejectionTimes
    query_project_message(prom->project_name,*prom);
    string s;
    s=to_string(prom->rejectionTimes); // wz changed it to note mode
    // sprintf(s, "%d" &prom.rejectionTimes); // wz change to_string method to sprintf
    // string now_time ;
    cout<<"请输入实际结项时间"<<endl;
    cin>>prom->post_time;
    if(prom->post_time<=prom->project_end_t)
    {
        cout << prom->post_time << endl;
        prom->coefficient=pow(0.5,prom->rejectionTimes);
        if(prom->rejectionTimes==0){
            prom->details="验收全部合格，按期结项";
            cout <<  prom->details << endl;
        }
        else{
            prom->details="验收"+s+"次不合格，按期超差结项";
            cout <<  prom->details << endl;
        }
    }
    else
    {

        if(prom->rejectionTimes==0)
        {
            prom->coefficient=0.9;
            prom->details="验收全部合格，但未按期结项";
             cout <<  prom->details << endl;
        }
        else
        {
            prom->coefficient=0.9*pow(0.5,prom->rejectionTimes);
            prom->details="验收"+s+"次不合格，超期超差结项";//还是说不结项？？？
            cout <<  prom->details << endl;
       }
    }
    //根据project_name将post_time、coefficient、details入库project_message
    update_projectpost(*prom);
}
//计算收益
double earnings_calculate(double total_money,float coefficient)
{
   return coefficient*total_money;
}

//前期：建立项目
void project_set_up()
{
    struct Project_message prom;
    prom.team_num=0;
    //立项
    input_project_message(&prom);//输入项目基本信息
    //添加项目团队信息
    insert_project_manage(prom);
    string team_input_flag="y";
    do{
            struct Project_Team pt;
            pt.project_name = prom.project_name;
            pt.team_member_num=0;
            cout<<"请输入项目"<<prom.project_name<<"的团队id："<<endl;
            cin>>pt.team_id;
            prom.team_num++;
            cout<<"请输入团队"<<pt.team_id<<"的工作量系数"<<endl;
            cin>>pt.team_percent;
            insert_project_team_develop(pt);
            //添加团队成员信息
            string staff_message;
            query_all_staff_id( staff_message, 0 );//读取并显示数据库系统里的员工id和姓名
            // cout << staff_message << endl;
            // cout<<"请输入数据库所有成员id"<<endl;
            // cin>>staff_message;
            cout<<"现有员工信息如下："<<endl;
            cout<<staff_message<<endl;
            string flag_member_input="y";
            do{
                    struct Project_member pm;
                    pm.team_id=pt.team_id;
                    cout<<"请输入团队成员id(输入上述有效id中的一个)："<<endl;
                    cin>>pm.id;
                    pt.team_member_num++;
                    cout<<"请输入成员"<<pm.id<<"的工作量系数;"<<endl;
                    cin>>pm.work_percent;
                    //根据team_id\work_percent将pm入库have
                    cout <<  pm.id  << endl;
                    insert_project_team(pm);        
                    cout<<"继续输入成员（y）/退出请输入其它任意键):"<<endl;
                    cin>>flag_member_input;
                }while(flag_member_input=="y");
            cout<<"团队"<<pt.team_id<<"的成员录入完毕"<<endl;
            cout<<"团队"<<pt.team_id<<"共有"<<pt.team_member_num<<"名成员参与"<<endl;
            //根据project_name将team_id、team_percent、team_member_num入库develop（也即pt入库）
            update_project_team_develop(pt);
            cout<<"继续输入团队（y）/退出请输入其它任意键):"<<endl;
            cin>>team_input_flag;
    
        }while(team_input_flag=="y");
    cout<<"项目"<<prom.project_name<<"的团队录入完毕"<<endl;
    cout<<"项目"<<prom.project_name<<"共有"<<prom.team_num<<"个团队参与"<<endl;

    //周期制定
    project_period_make(&prom); //其中有test_note_time入库
    cout<<"项目"<<prom.project_name<<"共有"<<prom.node_num<<"个测试、验收节点"<<endl;
    //prom入库project_message
    update_project_manage(prom);
    
}
//中期：过程管理
void project_process_manage()
{
    
    cout<<"请输入要进行过程管理的项目名称"<<endl;
    string projectName;
    cin>>projectName;
    string all_test_node_time;
    //根据project_name从数据库获得该项目的所有节点时间all_test_node_time并显示
    query_test_note_time(all_test_node_time,projectName);
    cout<<all_test_node_time<< endl;
    string time_node_now;
    cout<<"请输入上述节点时间中的一个作为本次过程管理的节点时间：（XXXX-XX-XX）"<<endl;
    cin>>time_node_now;
    cout<<"请选择过程管理类型：（1-过程监控，2-测试，3-验收）"<<endl;
    int choice;
    cin>>choice;
    switch(choice)
    {
    case 1:
        //过程监控 
        project_process_monitor(time_node_now,projectName);
        break;
    case 2:
        //项目测试 
        ProjectTest(time_node_now,projectName);
        break;
    case 3:
        //项目验收
        int test_result,rejectionTimes;
        //根据 node_time_now 和 project_name 读取本次验收前的测试结果test_result、rejectionTimes
        query_test_result(projectName, time_node_now, test_result);
        query_rejectionTimes(projectName, rejectionTimes);
        // cout<<"请输入本节点的test_result和rejectionTimes"<<endl;
        // cin>>test_result;
        // cin>>rejectionTimes;
        ProjectCheck_Accept(test_result,rejectionTimes,time_node_now,projectName);
        break;
    default:
        cout<<"过程管理类型选择错误"<<endl;
        break;
    }
}
//后期 结项及收益管理
void project_post_earnings()
{
    struct Project_message prom ;
    cout<<"请输入要进行结项和收益计算的项目名称"<<endl;
    cin>>prom.project_name;    
    ProjectPost(&prom);//结项

    //收益管理
    //根据project_name从数据库获得该项目的project_fund，team_num,coefficient
    
    query_project_message(prom.project_name,prom);
    // cout<<"请输入project_fund、team_num和coefficient"<<endl;
    // double project_fund;
    // int teamNum;
    // float coefficient;
    // cin>>project_fund>>teamNum>>coefficient;

    double project_earnings=earnings_calculate(prom.project_fund,prom.coefficient);//计算项目收益
    prom.project_earnings =project_earnings;
    cout <<"项目"<<prom.project_name<<"项目收益为:" << prom.project_earnings << endl;

    //int teamNum=team_num;
    cout<<"该项目团队数量为："<<prom.team_num<<endl;
    //团队收益计算
    for(int i=0;i<prom.team_num;i++)
    {
        string teamID;
        cout<<"请输入要计算收益的团队id"<<endl;
        cin>>teamID;
        //根据team_id和project_name从数据库读取该团队的team_percent、team_member_num
        struct Project_Team pt;
        pt.team_id = teamID;
        query_team_percent(pt,teamID,prom.project_name);
        pt.team_earnings=earnings_calculate(project_earnings,pt.team_percent);
       
        cout <<"团队"<<teamID<<"在项目"<<prom.project_name<<"中收益为:" << pt.team_earnings << endl;
        //根据team_id和project_name将team_earnings入库develop
        update_team_earnings(pt, prom.project_name);
    
        
        //个人收益计算
        cout<<"团队"<<teamID<<"的成员数量为"<< pt.team_member_num<<endl;
        // int memberNum;
        // cin>>memberNum;//memberNum=team_member_num;//数据库读取team_member_num
        for(int j=0;j<pt.team_member_num;j++)
        {
            string ID;
            cout<<"请输入要计算收益的员工id"<<endl;
            cin>>ID;
            float work_precent;
            // cout<<"请输入个人工作量系数："<<endl;
            // cin>>workPercent;            
            //根据staff_id和team_id从数据库读取相应的 work_percent
            query_work_percent(ID,teamID,work_precent);
            double workerEarnings = earnings_calculate(project_earnings,work_precent);
            cout <<"员工"<<ID<<"在项目"<<prom.project_name<<"中收益为:" << workerEarnings << endl;
            //根据team_id和staff_id将 worker_earnings 存入 have 表中
            update_worker_earnings( ID, teamID, workerEarnings);
        }
    }
    //根据project_name将project_earnings入库 project_message
    update_project_earnings(prom);


}

//项目管理系统入口函数
void project_manage_system()
{
    cout << "欢迎进入项目管理系统" << endl;

    cout<<"请选择项目管理阶段：（1-立项；2-监控、测试、验收；3-结项及收益计算）"<<endl;
    int choice;
    cin >> choice;
    switch (choice)
    {
		case 1:
		    project_set_up();
			break;
		case 2:
		    project_process_manage();
			break;
        case 3:
            project_post_earnings();
            break;
        default:
			cout<<"项目管理阶段选择错误"<<endl;
			break;
    }

    cout<<"感谢使用项目管理系统，再见！"<<endl;
}


// int main(){
//     initialize();

//     cout << "欢迎进入项目管理系统" << endl;
//     project_manage_system();
//     cout<<"项目管理系统演示结束！"<<endl;
//     int a;
//     cin>>a;

//     closeDB();
//     return 0;
// }